package com.dy.lock.screen.component.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Component;

import com.dy.lock.screen.component.HomePageRecommendComponent;
import com.dy.lock.screen.constants.TransmissionCode;
import com.dy.lock.screen.exception.ServerDisponseException;
import com.dy.lock.screen.pojo.HomePageRecomListShopMaterialInfo;
import com.dy.lock.screen.pojo.HomePageRecommendInfo;
import com.dy.lock.screen.scoket.Head;
import com.dy.lock.screen.service.HomePageRecomListShopMaterialRelationService;
import com.dy.lock.screen.service.HomePageRecommendService;
import com.dy.lock.screen.utils.CommonUtil;
import com.dy.lock.screen.utils.LocationUtil;
import com.dy.lock.screen.vo.HomeRecommendVo;
import com.google.gson.Gson;

@Component("homePageRecommendComponent")
public class HomePageRecommendComponentImpl implements
		HomePageRecommendComponent {

	private static final Logger log = Logger
			.getLogger(HomePageRecommendComponentImpl.class);
	private Gson gson;

	@Resource(name = "homePageRecommendService")
	private HomePageRecommendService homePageRecommendService;
	@Resource(name = "homePageRecomListShopMaterialRelationService")
	private HomePageRecomListShopMaterialRelationService homePageRecomListShopMaterialRelationService;

	public HomePageRecommendComponentImpl() {
		gson = CommonUtil.gson();
	}

	@Override
	public List<HomePageRecommendInfo> list(String body, Head head) {
		HomeRecommendVo vo = gson.fromJson(body, HomeRecommendVo.class);
		if (vo != null) {
			Integer currentPage = vo.getPageIndex();
			Integer pageSize = vo.getPageCount();
			Integer pageStart = 1;
			Integer pageEnd = 10;
			if (currentPage != null && pageSize != null) {
				pageStart = (currentPage - 1) * pageSize + 1;
				pageEnd = currentPage * pageSize;
			}
			Map<String, Object> paraMap = new HashMap<String, Object>();
			paraMap.put("pageStart", pageStart);
			paraMap.put("pageEnd", pageEnd);
			List<HomePageRecommendInfo> homeList = this.homePageRecommendService
					.queryPage(paraMap);
			if (homeList != null && !homeList.isEmpty()) {
				for (HomePageRecommendInfo info : homeList) {
					if (StringUtils.isNotBlank(info.getRecommendPic())) {
						info.setRecommendPic(CommonUtil.getUrlPrefix()
								+ info.getRecommendPic());
					}
					if (StringUtils.isNotBlank(info.getRecommendBackPic())) {
						info.setRecommendBackPic(CommonUtil.getUrlPrefix()
								+ info.getRecommendBackPic());
					}
				}
				return homeList;
			} else {
				return new ArrayList<HomePageRecommendInfo>();
			}

		} else {
			log.info("requestId:" + head.getRequestId() + ",获取首页推荐列表时，报文解析异常。");
			throw new ServerDisponseException(TransmissionCode.ERROR_ADJUST
					.getCode(), TransmissionCode.ERROR_ADJUST.getMessage());
		}
	}

	@Override
	public List<HomePageRecomListShopMaterialInfo> getMaterialListOfRecommend(
			String body, Head head) {
		HomeRecommendVo vo = gson.fromJson(body, HomeRecommendVo.class);
		if (vo != null) {
			Integer currentPage = vo.getPageIndex();
			Integer pageSize = vo.getPageCount();
			Integer pageStart = 1;
			Integer pageEnd = 10;
			if (currentPage != null && pageSize != null) {
				pageStart = (currentPage - 1) * pageSize + 1;
				pageEnd = currentPage * pageSize;
			}
			Float lng = vo.getLng();
			Float lat = vo.getLat();
			Map<String, Object> paraMap = new HashMap<String, Object>();
			paraMap.put("pageStart", pageStart);
			paraMap.put("pageEnd", pageEnd);
			paraMap.put("recommendId", vo.getRecommendId());
			List<HomePageRecomListShopMaterialInfo> homeList = this.homePageRecomListShopMaterialRelationService
					.queryPage(paraMap);
			if (homeList != null && !homeList.isEmpty()) {
				for (HomePageRecomListShopMaterialInfo info : homeList) {
					if (StringUtils.isNotBlank(info.getRecommendBackPic())) {
						info.setRecommendBackPic(CommonUtil.getUrlPrefix()
								+ info.getRecommendBackPic());
					}
					if (StringUtils.isNotBlank(info.getMaterialPic())) {
						info.setMaterialPic(CommonUtil.getUrlPrefix()
								+ info.getMaterialPic());
					}
					if (StringUtils.isNotBlank(info.getVideoPath())) {
						info.setVideoPath(CommonUtil.getUrlPrefix()
								+ info.getVideoPath());
					}
					if (StringUtils.isNotBlank(info.getHeaderPath())) {
						info.setHeaderPath(CommonUtil
								.getUrlPrefixApi("photo.url.prefix.path.api")
								+ info.getHeaderPath());
					}
					if (lng != null && lat != null && info.getLng() != null
							&& info.getLat() != null) {
						double distance = LocationUtil.calDistance(lat, lng,
								info.getLat(), info.getLng());
						info.setDistance(distance);
					}
				}
				return homeList;
			} else {
				return new ArrayList<HomePageRecomListShopMaterialInfo>();
			}

		} else {
			log.info("requestId:" + head.getRequestId()
					+ ",获取首页推荐文章列表时，报文解析异常。");
			throw new ServerDisponseException(TransmissionCode.ERROR_ADJUST
					.getCode(), TransmissionCode.ERROR_ADJUST.getMessage());
		}
	}

}
